//
//  LESendRankingController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/11.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LESendRankingController.h"
#import "LERankingCell.h"
#import "LEStaffDetialController.h"
#import "MakerTools.h"
#import "LEStoreModel.h"
#import "LEStaffModel.h"

static NSString *const LERankingCellId = @"LERankingCell";

@interface LESendRankingController ()

@end

@implementation LESendRankingController

- (void)setupUI
{
    [super setupUI];
    self.title = @"送单排行榜";
    [self.tableView registerNib:[UINib nibWithNibName:@"LERankingCell" bundle:nil] forCellReuseIdentifier:LERankingCellId];
    [self initHeader];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:@"1" forKey:@"type"];
    LEStoreModel *model = [MakerTools readFromSandBox:Store_Info];
    [dict setObject:model.store_id forKey:@"store_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:ServiceOrderRankUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        NSString *error = nil;
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            error = datas[@"error"];
        }
        if (error)
        {
            [self addNoDataViewWithTipText:error];
        }else
        {
            [self hiddenNoDataView];
            
            NSArray *tempModel = [LEStaffModel mj_objectArrayWithKeyValuesArray:datas];
            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
            [weakSelf.tableView reloadData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LERankingCell *cell = [tableView dequeueReusableCellWithIdentifier:LERankingCellId forIndexPath:indexPath];
    LEStaffModel *model = self.dataSource[indexPath.row];
    [cell setData:model indexPath:indexPath];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEStaffDetialController *staffDetial = [[LEStaffDetialController alloc] init];
    LEStoreModel *storeModel = [MakerTools readFromSandBox:Store_Info];
    LEStaffModel *model = self.dataSource[indexPath.row];
    staffDetial.store_id = storeModel.store_id;
    staffDetial.service_id = model.service_id;
    [self.navigationController pushViewController:staffDetial animated:YES];
}

@end
